package dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import po.Employment;
import po.Order;
import po.User;



public class UserDao extends BaseHibernateDAO{
	private Log log = LogFactory.getLog(UserDao.class);
	
	//用于登陆判断的dao
	public List findByHql(String hql) {
		log.debug("finding LoginUser instance by hql");
		//读取 Hibernate 配置文件 生成 SessionFactory，对Session进行管理
		SessionFactory sf= new Configuration().configure().buildSessionFactory();
		
		//打开一个session
		Session session=sf.openSession();    
		try {
			String queryString = hql;    
			Query queryObject = session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by hql failed", re);
			throw re;
		} finally{
			//对应打开 这里关闭
			session.close();
		}
	}
	//用户注册
	public void register(User user) {
		try {
			getSession().save(user);
			System.out.println("注册成功！");
			
		} catch (RuntimeException re) {
		
			throw re;
		}
	}
	
	//用户发布招聘信息
	public void issue(Employment emp) {
		log.debug("issue order");
		try {
			getSession().save(emp);
			System.out.println("用户发布招聘信息成功！");
			log.debug("issue successful");
		} catch (RuntimeException re) {
			log.error("issue failed", re);
			throw re;
		}
	}
	public void update(User user) {
		try {
			getSession().update(user);	
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	public User getUser(int id) {
		try {
			return getSession().find(User.class, id);			
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
